#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdbool.h>
#include "../lib/qut_pack.h"
#include "../lib/crypt.h"
#include <string.h>
#include <netinet/in.h>

char*	urltype[]		= {
	/*0 - 4*/"未知", "广告", "烟酒", "其它", "艺术",
	/*5 - 9*/"企业", "交通", "聊天", "Unknow_8", "论坛和新闻",
	/*10-14*/"仲裁", "计算机技术", "犯罪活动", "交友", "下载网站",
	/*15-19*/"教育", "娱乐", "财经", "赌博", "游戏",
	/*20-24*/"政府", "仇恨", "健康与医疗", "毒品", "求职",
	/*25-29*/"Unknow_25", "多媒体与下载", "新闻", "非营利及非政府机构", "裸体",
	/*30-34*/"个人网站", "网络钓鱼和欺诈", "政治", "色情", "房地产",
	/*35-39*/"宗教", "饮食与餐饮", "搜索引擎和门户网站", "购物", "社会网络",
	/*40-44*/"垃圾网站", "体育", "恶意软件", "Unknow_43", "翻译",
	/*45-49*/"旅游", "暴力", "武器", "Web电子邮箱", "一般",
	/*50-54*/"休闲娱乐", "Unknow_51", "Unknow_52", "Unknow_53", "Unknow_54",
	/*55-59*/"Unknow_55", "Unknow_56", "Unknow_57", "Unknow_58", "Unknow_59",
	/*60-64*/"Unknow_60", "僵尸机器", "邪教", "时尚与美容", "贺卡",
	/*65-69*/"黑客", "Unknow_66", "非法软件", "影像分享", "信息安全",
	/*70-74*/"限时消息", "网络错误", "域名停用", "P2P", "私有IP",
	/*75-79*/"考试作弊", "性教育", "不雅", "虐待儿童图像", "Unknow_79",
	"获取失败"
};

int main(int argc, char* argv[])
{
	int						fd;
	char					data[1024]		= {0};
	char					url[1024]		= {0};
	char*					purl			= NULL;//argv[1];
	qut_pack_t*				ppack			= (qut_pack_t *)data;
	int						len				= 0;//8 + 1 + strlen(purl);
	struct sockaddr_in		addr			= {0};
	struct sockaddr_in		addr_recv		= {0};
	int						type;

	fd = socket(AF_INET, SOCK_DGRAM, 0);
	while( 1 )
	{
		printf(">");
		if( !gets(url) )
		{
			break;
		}

		if(strcmp("exit", url) == 0)
		{
			break;
		}

		purl = url;
		len = 8 + 1 + strlen(purl);

		ppack->version = 2;
		ppack->service = 0;
		strcpy(ppack->url, purl);
		ppack->url_size = strlen(purl);

		des_encrypt("quts2053", &data[4], len, &len);
		ppack->des_size = htons(len);
	
		addr.sin_family = AF_INET;
		addr.sin_port = htons(2053);
		addr.sin_addr.s_addr = inet_addr("127.0.0.1");

		sendto(fd, data, 4 + len, 0, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
		// 接收数据
		len = sizeof(struct sockaddr_in);
		memset(data, 0, sizeof(data));
		recvfrom(fd, data, sizeof(data), 0, (struct sockaddr*)&addr_recv
			, &len);
	
		des_decrypt("quts2053", &data[4], ntohs(ppack->des_size), &len);
		type=ppack->head.res.url_tp1;
		if(type < 0 || type > 80)
		{
			type = 80;
		}
		printf("\"%s\" type is: %s\n", purl, urltype[type]);
	}	

	close(fd);
}
